//
//  MHCategoryDetailViewController.m
//  ManHua
//
//  Created by 赵永泉 on 15/10/26.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHCategoryDetailViewController.h"
#import "MHComicIntroductionCell.h"
#import "MHComicIntroduct.h"
#import <AFNetworking.h>
#import "MJRefresh.h"
#import "MHDataManager.h"

@interface MHCategoryDetailViewController ()
@property (nonatomic, strong) NSMutableArray *comicIntroductListArray;
@property (nonatomic, copy) NSString *acquireSite;
@property(nonatomic,strong)NSString *pageIndex;

@end

@implementation MHCategoryDetailViewController

static NSString * const reuseIdentifier = @"ComicIntroduction";

-(NSMutableArray *)comicIntroductListArray
{
    if (_comicIntroductListArray==nil) {
        self.comicIntroductListArray=[NSMutableArray arrayWithCapacity:0];
    }
    return _comicIntroductListArray;
}

- (void)acquireData
{


    [[AFHTTPRequestOperationManager manager] GET:self.acquireSite parameters:[NSDictionary dictionaryWithObjects:@[[NSString stringWithFormat:@"%ld", (long)_CatalogID], @"booklist", self.pageIndex, @"24", @"0", @"0"] forKeys:@[@"cataID", @"method", @"pageIndex", @"pageSize", @"state", @"order"]] success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if ([self.pageIndex isEqualToString: @"0"]) {
            [self.comicIntroductListArray removeAllObjects];
        }
        NSMutableArray *categoryListDataArr=[NSMutableArray arrayWithCapacity:0];
        categoryListDataArr =  [[MHDataManager shareManager]getCategoryListData:@"COMICINTRODUCTLIST" CategoryID:_CatalogID];
        if (categoryListDataArr) {
            [[MHDataManager shareManager]deleteFromTable:@"COMICINTRODUCTLIST" CategoryID:_CatalogID];
        }
        for (NSDictionary *dict in responseObject[@"Booklist"]) {
            MHComicIntroduct *comicIntroduct = [MHComicIntroduct modelWithDict:dict];
            [self.comicIntroductListArray addObject:comicIntroduct];
            [[MHDataManager shareManager]insertValueToTabel:@"COMICINTRODUCTLIST" ComicIntroduce:comicIntroduct categoryID:_CatalogID];
        }
        [self.collectionView reloadData];
        [self.collectionView.header endRefreshing];
        [self.collectionView.footer endRefreshing];
        
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        NSLog(@"%@", error.localizedDescription);
    }];
}

- (void)setCatalogID:(NSInteger)CatalogID
{
    _CatalogID = CatalogID;
    self.acquireSite = @"http://112.124.96.190:9090/bukamanhua5appjobs/ComicHandle.ashx";
    self.comicIntroductListArray=  [[MHDataManager shareManager]getCategoryListData:@"COMICINTRODUCTLIST" CategoryID:CatalogID];
    NSLog(@"%ld",self.comicIntroductListArray.count);
    [self.collectionView reloadData];
    [self acquireData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.pageIndex=@"0";

    [self.collectionView registerNib:[UINib nibWithNibName:@"MHComicIntroductionCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
    CGFloat margin = 20;
    CGFloat padding = 10;
    NSInteger row = 3;
    CGFloat itemW = (Width - 2 * padding - (row - 1) * padding) / row;
    CGFloat itemH = (itemW - padding) * 11.0 / 9.0 + 35;
    flowLayout.itemSize = CGSizeMake(itemW, itemH);
    flowLayout.sectionInset = UIEdgeInsetsMake(margin, padding, margin + 44, padding);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 10;
    
    __weak MHCategoryDetailViewController *test = self;
    self.collectionView.header =[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [test acquireData];
    }];
    self.collectionView.footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        test.pageIndex=[NSString stringWithFormat:@"%d", [self.pageIndex integerValue] + 1];
        [test acquireData];
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.comicIntroductListArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MHComicIntroductionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.comicIntroduct = self.comicIntroductListArray[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.pushComicDetailVCBlock != nil) {
        self.pushComicDetailVCBlock(self.comicIntroductListArray[indexPath.row]);
    }
}

@end
